/*
 * @describe 用户信息Dao文件
 * @author yisin
 * @date 2015-03-29 20:21:55
 */
package com.yisin.pbox.www.userInfo.dao;

import java.sql.SQLException;
import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.yisin.pbox.bean.pojo.system.UserInfo;
import com.yisin.pbox.frame.common.Response;
import com.yisin.pbox.frame.common.StatusCode;
import com.yisin.pbox.frame.log.ILogger;
import com.yisin.pbox.frame.log.LogFactory;
import com.yisin.pbox.www.userInfo.IUserInfoDao;

/**
 * <pre>
 * 用户信息 Dao
 * </pre>
 * 
 * @author yisin
 * @date 2015-03-29 20:21:55
 */
@Repository("userInfoDao")
public class UserInfoDao extends SqlSessionDaoSupport implements IUserInfoDao {
	private static ILogger logger = LogFactory
			.getDefaultLogger(UserInfoDao.class);

	/**
	 * <pre>
	 * 根据ID查询记录
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2015-03-29 20:21:55
	 */
	public Response getDataById(int id) throws SQLException {
		logger.debug("In getDataById");
		Response res = new Response();
		UserInfo userInfo = (UserInfo) getSqlSession().selectOne(
				"UserInfoMapper.getDataById", id);
		if (userInfo != null) {
			res.setStatusCode(StatusCode.SUCCESS);
			res.setObject(userInfo);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 加载全部 / 模糊搜索用户信息数据
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2015-03-29 20:21:55
	 */
	public Response loadAndSearch(UserInfo userInfo) throws SQLException {
		logger.debug("In loadAndSearch");
		Response res = new Response();
		List<UserInfo> list = getSqlSession().selectList(
				"UserInfoMapper.loadAndSearch", userInfo);
		if (list != null) {
			res.setStatusCode(StatusCode.SUCCESS);
			res.setList(list);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 高级搜索用户信息数据
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2015-03-29 20:21:55
	 */
	public Response advancedSearch(UserInfo userInfo) throws SQLException {
		logger.debug("In advancedSearch");
		Response res = new Response();
		List<UserInfo> list = getSqlSession().selectList(
				"UserInfoMapper.advancedSearch", userInfo);
		if (list != null) {
			res.setStatusCode(StatusCode.SUCCESS);
			res.setList(list);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}
	
	/**
	 * <pre>
	 * 精确查询用户信息数据
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2015-03-29 20:21:55
	 */
	public Response selectDataForAccurate(UserInfo userInfo) throws SQLException {
		logger.debug("In selectDataForAccurate");
		Response res = new Response();
		List<UserInfo> list = getSqlSession().selectList(
				"UserInfoMapper.selectDataForAccurate", userInfo);
		if (list != null) {
			res.setStatusCode(StatusCode.SUCCESS);
			res.setList(list);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 新增用户信息记录
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2015-03-29 20:21:55
	 */
	public Response insertData(UserInfo userInfo) throws SQLException {
		logger.debug("In insertData");
		Response res = new Response();
		int status = getSqlSession().insert("UserInfoMapper.insertData", userInfo);
		// 如果需要带出刚插入这条记录的ID
		// 则mapper中需要设置<insert id="insert" parameterType="UserInfo" useGeneratedKeys="true" keyProperty="id">，然后从实体id中获取
		if (status > 0) {
			res.setStatusCode(StatusCode.SUCCESS);
			res.setId(userInfo.getId());
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 修改用户信息信息
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2015-03-29 20:21:55
	 */
	public Response updateData(UserInfo userInfo) throws SQLException {
		logger.debug("In updateData");
		Response res = new Response();
		int status = getSqlSession().insert("UserInfoMapper.updateData", userInfo);
		if (status > 0) {
			res.setStatusCode(StatusCode.SUCCESS);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 删除用户信息
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2015-03-29 20:21:55
	 */
	public Response deleteData(UserInfo userInfo) throws SQLException {
		logger.debug("In deleteData");
		Response res = new Response();
		int status = getSqlSession().insert("UserInfoMapper.deleteData", userInfo);
		if (status > 0) {
			res.setStatusCode(StatusCode.SUCCESS);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 审核用户信息
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2015-03-29 20:21:55
	 */
	public Response auditingData(UserInfo userInfo) throws SQLException {
		logger.debug("In auditingData");
		Response res = new Response();
		int status = getSqlSession().insert("UserInfoMapper.auditingData", userInfo);
		if (status > 0) {
			res.setStatusCode(StatusCode.SUCCESS);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}
}
